home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / dev / lang / sbp3_1e.lzh / TOKENIZE.PL < prev    next >
Text File  |  1991-10-31  |  2KB  |  75 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5.  
  6. /* TOKENIZE.PL */
  7.  
  8. /**************************************************
  9.  * tokenize(String,Result)                        *
  10.  *  Converts String (a list of ASCII codes) into  *
  11.  *  into a list of atoms, discarding punctuation  *
  12.  *  and converting all letters to lower case.     *
  13.  **************************************************/
  14.  
  15. tokenize([],[]) :- !.
  16.  
  17. tokenize(String,[Word|Rest]) :-
  18.   grab_word(String,Chars,NewString),
  19.   name(Word,Chars),
  20.   tokenize(NewString,Rest).
  21.  
  22.  
  23. /*
  24.  * grab_word(String,Chars,Rest)
  25.  *  Splits String into the characters constituting
  26.  *  the first token (Chars) and the remainder (Rest).
  27.  */
  28.  
  29. grab_word([32|Tail],[],Tail) :- !.
  30.   /* Stop upon hitting a blank */
  31.  
  32. grab_word([],[],[]).
  33.   /* Stop upon hitting the end of the list */
  34.  
  35. grab_word([Char|Tail],Chars,Rest) :-
  36.   punctuation_mark(Char), !,
  37.   grab_word(Tail,Chars,Rest).
  38.   /* Skip punctuation marks */
  39.  
  40. grab_word([Char|Tail1],[NewChar|Tail2],Rest) :-
  41.   grab_word(Tail1,Tail2,Rest),
  42.   lower_case(Char,NewChar),
  43.   !.
  44.   /* If within a word, keep going. */
  45.   /* Cut rules out spurious alternatives. */
  46.  
  47.  
  48. /*
  49.  * punctuation_mark(Char)
  50.  *   succeeds if Char is a punctuation mark.
  51.  */
  52.  
  53. punctuation_mark(Char) :- Char =< 47.
  54. punctuation_mark(Char) :- Char >= 58, Char =< 64.
  55. punctuation_mark(Char) :- Char >= 91, Char =< 96.
  56. punctuation_mark(Char) :- Char >= 123.
  57.  
  58.  
  59. /*
  60.  * lower_case(Char,NewChar)
  61.  *  NewChar is the lower case character
  62.  *  corresponding to Char.
  63.  */
  64.  
  65. lower_case(Char,NewChar) :-
  66.    Char >= 65,
  67.    Char =< 90,
  68.    NewChar is Char+32.
  69.  
  70. lower_case(Char,Char) :-
  71.    Char < 65.
  72.  
  73. lower_case(Char,Char) :-
  74.    Char > 90.
  75.